package com.rules.admin.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.rules.admin.entity.*;
import com.rules.admin.service.*;
import com.rules.admin.utils.ResultUtils;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RequestMapping({"/data_dict"})
@Controller
public class DataDictController {
   @Autowired
   private DataDictService dataDictService;
   @Autowired
   private DataSourceModelService dataSourceModelService;
   @Autowired
   private DubboService dubboService;
   @Autowired
   private DataDictDetailService dataDictDetailService;
   @Autowired
   private SqlConfigModelService sqlConfigModelService;
   @Autowired
   private ProjectService projectService;
   @Autowired
   private RulePackageService rulePackageService;

   @RequestMapping(
      method = {RequestMethod.GET}
   )
   @ResponseBody
   public IPage<DataDictModel> queryPage(IPage<DataDictModel> page, DataDictModel dataDictModel) {
      return this.dataDictService.queryPage(dataDictModel.getDataName(), page);
   }

   @RequestMapping(
      value = {"/{id}"},
      method = {RequestMethod.GET}
   )
   @ResponseBody
   public DataDictModel getRule(@PathVariable("id") Long id) {
      return this.dataDictService.getById(id);
   }

   @RequestMapping(
      method = {RequestMethod.POST}
   )
   @ResponseBody
   public Map<String, Object> addDataDict(@RequestBody DataDictModel dataDictModel) {
      this.dataDictService.save(dataDictModel);
      return ResultUtils.getSuccessResultData();
   }

   @RequestMapping(
      value = {"/{id}"},
      method = {RequestMethod.PUT}
   )
   @ResponseBody
   public Map<String, Object> updateRule(@RequestBody DataDictModel dataDictModel) {
      this.dataDictService.updateById(dataDictModel);
      return ResultUtils.getSuccessResultData();
   }

   @RequestMapping(
      value = {"/{id}"},
      method = {RequestMethod.DELETE}
   )
   @ResponseBody
   public Map<String, Object> deleteDataDict(@PathVariable("id") Long id) {
      this.dataDictService.removeById(id);
      return ResultUtils.getSuccessResultData();
   }

   @RequestMapping(
      value = {"/rule/dict/{id}"},
      method = {RequestMethod.GET}
   )
   @ResponseBody
   public Map<String, Object> getRuleDict(@PathVariable("id") String projectId) {
      List<Map<String, Object>> result = new ArrayList();
      List<DubboModel> dubboConfigList = this.dubboService.getListByProjectCode(projectId);
      List<DataSourceModel> dataSourceModels = this.dataSourceModelService.selectListByProjectId(projectId);
      List<Project> projects = this.projectService.getAll();
      List<RulePackage> rulePackages = this.rulePackageService.getPackage();
      List<SqlConfigModel> sqlConfigModelList = new ArrayList();
      Iterator var8 = dataSourceModels.iterator();

      while(var8.hasNext()) {
         DataSourceModel dataSourceModel = (DataSourceModel)var8.next();
         sqlConfigModelList.addAll(this.sqlConfigModelService.selectListByDataSourceId(dataSourceModel.getId().toString()));
      }

      List<Map<String, String>> dubboList = new ArrayList();
      Iterator var26 = dubboConfigList.iterator();

      while(var26.hasNext()) {
         DubboModel dubboModel = (DubboModel)var26.next();
         Map<String, String> map = new HashedMap();
         map.put("name", dubboModel.getDubboName());
         map.put("code", dubboModel.getDubboName());
         dubboList.add(map);
      }

      List<Map<String, String>> sqlList = new ArrayList();
      Iterator var28 = sqlConfigModelList.iterator();

      while(var28.hasNext()) {
         SqlConfigModel sqlConfigModel = (SqlConfigModel)var28.next();
         Map<String, String> map = new HashedMap();
         map.put("name", sqlConfigModel.getSqlName());
         map.put("code", sqlConfigModel.getSqlName());
         sqlList.add(map);
      }

      List<Map<String, String>> projectList = new ArrayList();
      Iterator var31 = projects.iterator();

      while(var31.hasNext()) {
         Project project = (Project)var31.next();
         Map<String, String> map = new HashedMap();
         map.put("name", project.getProjectName());
         map.put("code", project.getProjectCode());
         projectList.add(map);
      }

      List<Map<String, String>> rulePackageList = new ArrayList();
      Iterator var34 = rulePackages.iterator();

      while(var34.hasNext()) {
         RulePackage rulePackage = (RulePackage)var34.next();
         Map<String, String> map = new HashedMap();
         map.put("name", rulePackage.getName());
         map.put("code", rulePackage.getArtifactId());
         rulePackageList.add(map);
      }

      Map dubboMap = new HashMap();
      dubboMap.put("name", "dubbo服务");
      dubboMap.put("show", false);
      dubboMap.put("records", dubboList);
      result.add(dubboMap);
      Map sqlMap = new HashMap();
      sqlMap.put("name", "JDBC服务");
      sqlMap.put("show", false);
      sqlMap.put("records", sqlList);
      result.add(sqlMap);
      Map projectMap = new HashMap();
      projectMap.put("name", "项目");
      projectMap.put("show", false);
      projectMap.put("records", projectList);
      result.add(projectMap);
      Map rulePackageMap = new HashMap();
      rulePackageMap.put("name", "规则包");
      rulePackageMap.put("show", false);
      rulePackageMap.put("records", rulePackageList);
      result.add(rulePackageMap);
      List<DataDictModel> dictlist = this.dataDictService.getAll();
      Iterator var17 = dictlist.iterator();

      while(var17.hasNext()) {
         DataDictModel dataDictModel = (DataDictModel)var17.next();
         Map dictMap = new HashMap();
         List list = new ArrayList();
         dictMap.put("name", dataDictModel.getDataName());
         dictMap.put("show", false);
         dictMap.put("records", list);
         List<DataDictDetailModel> dictDetailModels = this.dataDictDetailService.getAllByDataId(dataDictModel.getId());
         Iterator var22 = dictDetailModels.iterator();

         while(var22.hasNext()) {
            DataDictDetailModel dataDictDetailModel = (DataDictDetailModel)var22.next();
            Map dictDetailMap = new HashMap();
            dictDetailMap.put("name", dataDictDetailModel.getDataName());
            dictDetailMap.put("code", dataDictDetailModel.getDataCode());
            list.add(dictDetailMap);
         }

         result.add(dictMap);
      }

      System.out.println(result.toString());
      return ResultUtils.getSuccessResultData(result);
   }
}
